//
//  Task_Views_AppDelegate.m
//  Task Views
//
//  Created by Chris Karr on 2/17/09.
//  Copyright Chris J. Karr 2009 . All rights reserved.
//

#import "Task_Views_AppDelegate.h"
#import "HideValueTransformer.h"

#import "ASConduit.h"
#import "ASFilter.h"
#import "ASConfiguration.h"

@implementation Task_Views_AppDelegate

- (id) init
{
	if (self = [super init])
	{
		HideValueTransformer * t = [[[HideValueTransformer alloc] init] autorelease];
		
		[NSValueTransformer setValueTransformer:t forName:@"HideValueTransformer"];
	}
	
	return self;
}

- (NSString *) applicationSupportFolder 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains (NSApplicationSupportDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : NSTemporaryDirectory ();
    return [basePath stringByAppendingPathComponent:@"Task Views"];
}

- (void) awakeFromNib
{
	NSFileManager * manager = [NSFileManager defaultManager];
	NSString * file = [[self applicationSupportFolder] stringByAppendingPathComponent:@"Database.bin"];
	
	if ([manager fileExistsAtPath:file])
	{
		NSSet * savedTasks = [NSUnarchiver unarchiveObjectWithFile:file];
		
		for (NSMutableDictionary * task in savedTasks)
			[tasks addObject:task];
	}

	[[NSNotificationCenter defaultCenter] postNotificationName:@"refresh_fields" object:nil];
}

- (IBAction) saveAction:(id)sender 
{

}

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender 
{
	[filters setSelectedObjects:[NSArray array]];
	
	NSData * data = [NSKeyedArchiver archivedDataWithRootObject:[tasks arrangedObjects]];
		
	[data writeToFile:[taskManager taskDataLocation] atomically:YES];
	
    return NSTerminateNow;
}

- (IBAction) preferences:(id) sender
{
	[preferences makeKeyAndOrderFront:sender];
}

- (BOOL)application:(NSApplication *) sender delegateHandlesKey:(NSString *) key
{
	if ([key isEqual:@"conduits"])
		return YES;
	else if ([key isEqual:@"filters"])
		return YES;
	else if ([key isEqual:@"configurations"])
		return YES;
	
	return NO;
}

- (NSArray *) conduits
{
	NSMutableArray * conduitArray = [NSMutableArray array];

	for (NSDictionary * conduit in [conduits arrangedObjects])
	{
		ASConduit * dict = [ASConduit dictionary];
		
		[dict setValue:[conduit valueForKey:@"name"] forKey:@"name"];
		[dict setValue:[conduit valueForKey:@"type"] forKey:@"type"];
		[dict setValue:conduit forKey:@"source"];
		
		[conduitArray addObject:dict];
	}
	
	return conduitArray;
}

- (NSArray *) filters
{
	NSMutableArray * filterArray = [NSMutableArray array];
	
	for (NSDictionary * filter in [filters arrangedObjects])
	{
		ASFilter * dict = [ASFilter dictionary];
		
		[dict setValue:[filter valueForKey:@"name"] forKey:@"name"];
		
		id predicate = [NSKeyedUnarchiver unarchiveObjectWithData:[filter valueForKey:@"filter"]];
		
		[dict setValue:[predicate predicateFormat] forKey:@"definition"];
		[dict setValue:filter forKey:@"source"];
		
		[filterArray addObject:dict];
	}
	
	return filterArray;
}

- (NSArray *) configurations
{
	NSMutableArray * configArray = [NSMutableArray array];
	
	for (NSDictionary * config in [configurations arrangedObjects])
	{
		ASConfiguration * dict = [ASConfiguration dictionary];
		
		[dict setValue:[config valueForKey:@"name"] forKey:@"name"];
		[dict setValue:config forKey:@"source"];
		
		[configArray addObject:dict];
	}
	
	return configArray;
}


@end
